<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Answers extends MY_Controller {
    
    function __construct()
    {
        parent :: __construct();
        $this->lang->load('answers', 'chinese');
        $this->load->model(array('Answers_model','Comments_model'));
    }
    
    public function index()
    {
        $this->setHeader('title', '分类');
        $this->navigation();
        
        $parm['where'] = array();
        $thisData['answers'] = $this->Answers_model->getAnswers($parm);
        $thisData['tags'] = $this->tags();
        $thisData['hot'] = $this->hots();
        $thisData['culture'] = $this->culture();
        $thisData['policy'] = $this->policy();
        $thisData['news'] = $this->news();
        $this->load->view('answers', $thisData);
    }
    
    public function detail()
    {
        
        $thisData['answers_detail'] = $this->Answers_model->getOne($this->uri->segment(3));
        
        $this->setHeader('title', $thisData['answers_detail']['answers_title']);
        $this->setHeader('description', $thisData['answers_detail']['answers_content']);
        $this->navigation();
        
        $parm['where'] = array(
            'cid'=>$this->uri->segment(3),
            'type'=>'answers',
            'status'=>'approved'
            );
        $thisData['answers_comments'] = $this->Comments_model->getComments($parm);
        //print_r($thisData);
        $thisData['culture'] = $this->culture();
        $thisData['policy'] = $this->policy();
        $thisData['news'] = $this->news();
        $thisData['merchandises'] = $this->merchandises();
        $thisData['answers'] = $this->answers();
        $thisData['placesymbols'] = $this->placesymbols();
        
        $this->load->view('answers_detail', $thisData);
    }
    
    public function create()
    {
        $this->navigation();
        $this->load->view('answers_create');
    }
    
    public function do_save()
    {
        $validation = array(
            array(
                'field' => 'answers_title',
                'label' => lang('answers_title'),
                'rules' => 'required|trim'
            ),
            array(
                'field' => 'answers_content',
                'label' => lang('answers_content'),
                'rules' => 'required|trim'
            )
        );

        // Set the validation rules
        $this->form_validation->set_rules($validation);
        $user_data = array();
        // Convert the array to an object
        $user_data['answers_title']     = $this->security->xss_clean($this->input->post('answers_title'));
        $user_data['answers_content']   = $this->security->xss_clean($this->input->post('answers_content'));
        $user_data['answers_tag']   	= $this->security->xss_clean($this->input->post('answers_tag'));
        $user_data['answers_ip']        = $this->input->ip_address();
        
        if ($this->form_validation->run())
        {
            if($this->Answers_model->add($user_data))
                $this->goback(1);
            else
                $this->goback(0);
        }
        else
        {
            echo validation_errors(); 
        }
    }
    
    public function do_save_reply()
    {
        $validation = array(
            array(
                'field' => 'answers_name',
                'label' => lang('answers_name'),
                'rules' => 'required|trim'
            ),
            array(
                'field' => 'answers_content',
                'label' => lang('answers_content_answer'),
                'rules' => 'required|trim'
            ),
            array(
                'field' => 'answers_mail',
                'label' => lang('answers_mail'),
                'rules' => 'trim|valid_email'
            )
        );

        // Set the validation rules
        $this->form_validation->set_rules($validation);
        $user_data = array();
        // Convert the array to an object
        $user_data['answers_url']       = prep_url($this->security->xss_clean($this->input->post('answers_url')));
        $user_data['answers_content']   = $this->security->xss_clean($this->input->post('answers_content'));
        $user_data['answers_name']   	= $this->security->xss_clean($this->input->post('answers_name'));
        $user_data['answers_mail']   	= $this->security->xss_clean($this->input->post('answers_mail'));
        $user_data['answers_id']   	    = $this->security->xss_clean($this->input->post('answers_id'));
        $user_data['answers_ip']        = $this->input->ip_address();
        $user_data['answers_agent']     = $this->input->user_agent();
        $user_data['answers_type']      = 'answers';
        
        if ($this->form_validation->run())
        {
            if($this->Comments_model->add($user_data))
                redirect('answers/detail/'.$user_data['answers_id']);
            else
                $this->goback(0);
        }
        else
        {
            echo validation_errors(); 
        }
    }
}